<?php
/**
 * Desc: Service模板
 * User: zdb
 * Date: 2023/6/5
 * Time: 11:22
 */

namespace app\api\service\project;
use think\facade\Db;

class ProjectTaskService
{

    private $primaryTable;

    public function __construct()
    {
        $this->primaryTable = "yj_project_task";
    }

    public function findRowById($id)
    {
        return Db::table($this->primaryTable)->alias('a')
            ->where('id', '=', $id)
            ->strict(false)
            ->find();
    }

    public function findRowByData($data)
    {
        $condition = $this->assembleFilterCondition($data);
        return Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->strict(false)
            ->find();
    }

    public function getListByManyId($arrId)
    {
        $list = Db::table($this->primaryTable)->alias('a')
            ->whereIn('id', $arrId)
            ->select()
            ->toArray();
        return $list;
    }

    public function assembleFilterCondition($data)
    {
        $condition = ['a.status = 1'];

        if (!empty($data['title'])) {
            $condition[] = "title like '%" . $data['title'] . "%'";
        }

        if (!empty($data['project_basic_id'])) {
            $condition[] = "pmt.project_basic_id =".$data['project_basic_id'];
        }

        if (!empty($data['module_branch_id'])) {
            $condition[] = "pmb.id =".$data['module_branch_id'];
        }

        return empty($condition) ? "" : implode(" AND ", $condition);
    }

    public function getListNoPage($data = [], $limitNum = 30)
    {
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)->alias('a')
            ->leftJoin('yj_project_module_branch pmb', 'pmb.id = a.module_branch_id')
            ->leftJoin('yj_project_module_trunk pmt', 'pmt.id = pmb.module_trunk_id')
            ->leftJoin('yj_project_basic pb', 'pb.id = pmt.project_basic_id')
            ->leftJoin('yj_project_trunk pt', 'pt.id = pb.project_trunk_id')
            ->where($condition)
            ->limit(0, $limitNum)
            ->order('a.id desc')
            ->field("a.*, pt.project_name, pb.version, pmt.module_trunk_name, pmt.project_basic_id, pmb.module_branch_name")
            ->select()
            ->toArray();
        return $res;
    }

    public function getListWithPage($data, $pageIndex = 1, $pageSize = 30)
    {
        $offset = ($pageIndex - 1) * $pageSize;
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)->alias('a')
            ->leftJoin('yj_project_module_branch pmb', 'pmb.id = a.module_branch_id')
            ->leftJoin('yj_project_module_trunk pmt', 'pmt.id = pmb.module_trunk_id')
            ->leftJoin('yj_project_basic pb', 'pb.id = pmt.project_basic_id')
            ->leftJoin('yj_project_trunk pt', 'pt.id = pb.project_trunk_id')
            ->where($condition)
            ->limit($offset, $pageSize)
            ->order('pmt.sort asc, pmt.id desc, pmb.sort asc, pmb.id desc, a.sort asc, a.id desc')
            ->field("a.*, pt.project_name, pb.version, pmt.module_trunk_name, pmt.project_basic_id, pmb.module_branch_name")
            ->select();
        return $res;
    }

    public function getTotalNum($data)
    {
        $condition = $this->assembleFilterCondition($data);
        $num = Db::table($this->primaryTable)->alias('a')
            ->leftJoin('yj_project_module_branch pmb', 'pmb.id = a.module_branch_id')
            ->leftJoin('yj_project_module_trunk pmt', 'pmt.id = pmb.module_trunk_id')
            ->leftJoin('yj_project_basic pb', 'pb.id = pmt.project_basic_id')
            ->leftJoin('yj_project_trunk pt', 'pt.id = pb.project_trunk_id')
            ->where($condition)
            ->count();
        return $num;
    }

    public function createRow($data)
    {
        $data['status'] = isset($data['status']) ? $data['status']:1;
        $maxSort = $this->getMaxSort();
        $data['sort'] = $maxSort + 1;
        $data['plan_end_date'] = $data['plan_end_date'] ?: null;
        $res = Db::table($this->primaryTable)
            ->strict(false)
            ->insert($data, true);
        return $res;
    }

    public function createManyRow($data)
    {
        $res = Db::table($this->primaryTable)
            ->strict(false)
            ->duplicate($data)
            ->insertAll($data, true);
        return $res;
    }

    public function updateRowById($id, $data)
    {
        $data['plan_end_date'] = empty($data['plan_end_date']) ? null:$data['plan_end_date'];
        $res = Db::table($this->primaryTable)->alias('a')
            ->where('id', '=', $id)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function updateListByData($condition, $data)
    {
        $condition = $this->assembleFilterCondition($condition);
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function updateListByManyId($arrId, $data)
    {
        $res = Db::table($this->primaryTable)->alias('a')
            ->whereIn('a.id', $arrId)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeRowById($id)
    {
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)
            ->where('id', '=', $id)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeListByManyId($arrId)
    {
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)->alias('a')
            ->whereIn('a.id', $arrId)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeListByData($data)
    {
        $condition = $this->assembleFilterCondition($data);
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function getMaxSort(){
        $max = Db::table($this->primaryTable)->alias('a')
            ->where('a.status', '=', 1)
            ->max("sort");
        return empty($max) ? 0:$max;
    }
}